Page({
  data: {
    orderInfo: null
  },

  onLoad(options) {
    if (options.orderId) {
      this.loadOrderInfo(options.orderId)
    }
  },

  // 加载订单信息
  loadOrderInfo(orderId) {
    wx.cloud.callFunction({
      name: 'getOrder',
      data: { orderId: orderId },
      success: res => {
        if (res.result.success) {
          const order = res.result.order
          // 格式化支付时间
          const paidAt = order.paid_at ? new Date(order.paid_at).toLocaleString() : ''
          
          this.setData({
            orderInfo: {
              ...order,
              paid_at: paidAt
            }
          })
        }
      }
    })
  },

  // 查看订单
  viewOrder() {
    wx.redirectTo({
      url: `/pages/order-detail/order-detail?id=${this.data.orderInfo._id}`
    })
  },

  // 返回首页
  backToHome() {
    wx.switchTab({
      url: '/pages/home/home'
    })
  }
}) 